package cn.tedu.ems.service.impl;

import cn.tedu.ems.ex.ServiceException;
import cn.tedu.ems.mapper.NewsMapper;
import cn.tedu.ems.pojo.dto.NewsAddNewDTO;
import cn.tedu.ems.pojo.dto.NewsUpdateDTO;
import cn.tedu.ems.pojo.entity.News;
import cn.tedu.ems.pojo.vo.NewsListItemVO;
import cn.tedu.ems.pojo.vo.NewsStandardVO;
import cn.tedu.ems.service.INewsService;
import cn.tedu.ems.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;

    public NewsServiceImpl() {
        log.info("创建业务对象：NewsServiceImpl");
    }

    @Override
    public void addNew(NewsAddNewDTO newsAddNewDTO) {
        log.debug("开始处理【添加新闻】的业务，参数：{}", newsAddNewDTO);

        log.debug("即将检查文章标题是否被占用...");
        String title = newsAddNewDTO.getTitle();
        int count = newsMapper.countByTitle(title);
        if (count > 0) {
            String message = "添加新闻失败,该新闻标题已被占用!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        log.debug("新闻名称没有被占用，将向新闻表中插入数据");
        News news = new News();
        BeanUtils.copyProperties(newsAddNewDTO, news);
        log.debug("即将插入新闻数据：{}", news);
        int rows = newsMapper.insert(news);
        if (rows != 1) {
            String message = "添加新闻失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("插入新闻数据完成");
    }


    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据id删除新闻】的业务，参数：{}", id);
        NewsStandardVO queryResult = newsMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除新闻失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("即将执行删除，参数：{}", id);
        int rows = newsMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除新闻失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, NewsUpdateDTO newsUpdateDTO) { //编辑
        log.debug("开始处理【修改新闻详情】的业务，参数ID：{}, 新数据：{}", id, newsUpdateDTO);

        int count = newsMapper.countByTitleAndNotId(id,newsUpdateDTO.getTitle());
        if (count > 0) {
            String message = "修改新闻详情失败，相册名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        NewsStandardVO queryResult = newsMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "更新失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        News news = new News();
        BeanUtils.copyProperties(newsUpdateDTO,news);
        news.setId(id);

        log.debug("即将修改数据：{}", news);
        int rows = newsMapper.update(news);
        if (rows != 1) {
            String message = "更新失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public NewsStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询新闻详情】的业务，参数：{}", id);
        NewsStandardVO news = newsMapper.getStandardById(id);
        if (news == null) {
            String message = "获取新闻详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return news;
    }


    @Override
    public NewsStandardVO getStandardByTitle(String title) {
        log.debug("开始处理【根据id查询新闻详情】的业务，参数：{}", title);
        NewsStandardVO news = newsMapper.getStandardByTitle(title);
        if (news == null) {
            String message = "获取新闻详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return news;
    }

    @Override
    public List<NewsListItemVO> list() {
        log.debug("开始处理【查询新闻列表】的业务，无参数");
        return newsMapper.list();
    }

    @Override
    public List<NewsListItemVO> listByTitle(String title) {
        log.debug("开始处理【根据title查询新闻】的业务，参数：{}", title);
        return newsMapper.listByTitle(title);
    }

    @Override
    public List<NewsListItemVO> listByAuthor(String author) {
        log.debug("开始处理【根据author查询新闻】的业务，参数：{}", author);
        return newsMapper.listByAuthor(author);
    }

    @Override
    public List<NewsListItemVO> listBySubmissionDepartment(String submissionDepartment) {
        log.debug("开始处理【根据submissionDepartment查询新闻】的业务，参数：{}", submissionDepartment);
        return newsMapper.listBySubmissionDepartment(submissionDepartment);
    }

    @Override
    public List<NewsListItemVO> listBySubmissionColumn(String submissionColumn) {
        log.debug("开始处理【根据submissionColumn查询新闻】的业务，参数：{}", submissionColumn);
        return newsMapper.listBySubmissionColumn(submissionColumn);
    }
}
